#include <iostream>

using namespace std;

typedef long long LL;

const int N = 1e6 + 10;
const LL P = 1e9 + 7;

LL mis[N], f[N], g[N];
int T;

LL qpow(LL a, LL b, LL p)
{
	LL ret = 1;
	while(b)
	{
		if(b & 1) ret = ret * a % p;
		b >>= 1;
		a = a * a % p;
	}
	return ret;
}

void init()
{
	int n = N - 1;
	f[0] = 1;
	for(int i = 1; i <= n; i++) f[i] = f[i - 1] * i % P;
	
	g[n] = qpow(f[n], P - 2, P);
	for(int i = n - 1; i >= 0; i--) g[i] = (i + 1) * g[i + 1] % P;
//	for(int i = 1; i <= 10; i++) cout << g[i] << " ";

	mis[2] = 1;
	for(int i = 3; i <= n; i++) mis[i] = (i - 1) * (mis[i - 1] + mis[i - 2]) % P;
	
}

int main()
{
	init();
	scanf("%d", &T);
	while(T--)
	{
		int n, m; scanf("%d%d", &n, &m);
		if(n == m) printf("%d\n", 1);
		else
		{
			LL ret = f[n] * g[m] % P * g[n - m] % P * mis[n - m] % P;
			printf("%lld\n", ret);
		}
		
	}
	return 0;
}
